#include "image.h"
#include "stb_image.h"
#include "stb_image_write.h"


namespace uzu
{
    void ReadImage(const std::string& filepath, Image<uint8_t>& image, uint32_t desired_channels)
    {
        int width, height, channels;
        unsigned char* data = stbi_load(filepath.c_str(), &width, &height, &channels, desired_channels);
        image.width = width;
        image.height = height;
        image.channels = channels;
        image.data = data;
    }

    void WriteImage(const std::string& filepath, Image<uint8_t>& image)
    {
        if (image.device_type == DeviceType::CUDA)
            image.ToHost();
        uint32_t line_size = image.width * image.channels;
        stbi_write_jpg(filepath.c_str(), image.width, image.height, image.channels, image.data, line_size);
    }

}
